/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @author ben
 */
public class controller_ChuyenTau {

    public List<model_ChuyenTau> LoadList() throws Exception {
        try {
            DataProvider conn = new DataProvider();
            conn.Open();
            List<model_ChuyenTau> lst = new ArrayList<>();
            String query = "SELECT * FROM chuyen_tau";
            //Statement st = conn.createStatement();
            ResultSet rs = conn.ExecuteQuery(query);

            while (rs.next()) {
                model_ChuyenTau ct = new model_ChuyenTau();

                ct.setMACHUYEN(rs.getString("MaChuyen"));
                ct.setMATUYEN(rs.getString("MaTuyen"));
                ct.setMADT(rs.getString("MaDT"));
                ct.setTHOIGIANXP(rs.getDate("ThoiGianXP"));

                lst.add(ct);
            }
            rs.close();
            conn.Close();
            return lst;

        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
        return null;
    }

    public void Xoa_ChuyenTau(String machuyen) {
        DataProvider conn = new DataProvider();
        conn.Open();
        String query = String.format("DELETE FROM chuyen_tau WHERE MaChuyen = '%s'", machuyen);
        conn.ExecuteUpdate(query);
    }

    public int Them_ChuyenTau(model_ChuyenTau ct) {
        DataProvider conn = new DataProvider();
        conn.Open();

        String query = String.format("INSERT INTO chuyen_tau VALUES ('%s','%s','%s','%s')",
                ct.getMACHUYEN(),
                ct.getMATUYEN(),
                ct.getMADT(),
                ct.getTHOIGIANXP());

        return conn.ExecuteUpdate(query);
    }

    public int CapNhat_ChuyenTau(model_ChuyenTau ct) {
        DataProvider conn = new DataProvider();
        conn.Open();

        String query = String.format("UPDATE chuyen_tau SET MaChuyen = '%s', MaTuyen = '%s', MaDT = '%s', ThoiGianXP = '%s' where MaChuyen = '%s'",
                ct.getMACHUYEN(),
                ct.getMATUYEN(),
                ct.getMADT(),
                ct.getTHOIGIANXP(),
                ct.getMACHUYEN());

        return conn.ExecuteUpdate(query);
    }

    public model_ChuyenTau Load_CapNhat_ChuyenTau(String machuyen) throws SQLException {
        DataProvider conn = new DataProvider();
        conn.Open();

        String query = String.format("SELECT * FROM chuyen_tau WHERE MaChuyen = '%s'", machuyen);
        ResultSet rs = conn.ExecuteQuery(query);

        model_ChuyenTau ct = new model_ChuyenTau();

        while (rs.next()) {
            ct.setMACHUYEN(rs.getString("MaChuyen"));
            ct.setMATUYEN(rs.getString("MaTuyen"));
            ct.setMADT(rs.getString("MaDT"));
            ct.setTHOIGIANXP(rs.getDate("ThoiGianXP"));
        }

        return ct;
    }

    public List<model_ChuyenTau> TimKiem_ChuyenTau(String machuyen) throws SQLException {

        DataProvider conn = new DataProvider();
        conn.Open();

        List<model_ChuyenTau> lst = new ArrayList<>();
        String query = String.format("SELECT * FROM chuyen_tau WHERE MaChuyen = '%s'", machuyen);
        ResultSet rs = conn.ExecuteQuery(query);

        while (rs.next()) {
            model_ChuyenTau ct = new model_ChuyenTau();

            ct.setMACHUYEN(rs.getString("MaChuyen"));
            ct.setMATUYEN(rs.getString("MaTuyen"));
            ct.setMADT(rs.getString("MaDT"));
            ct.setTHOIGIANXP(rs.getDate("ThoiGianXP"));

            lst.add(ct);
        }
        rs.close();
        conn.Close();
        return lst;
    }
}
